package com.example.f8399.testdemo.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.f8399.testdemo.BaseActivity;
import com.example.f8399.testdemo.R;
import com.example.f8399.testdemo.adapter.RcAdapter;
import com.example.f8399.testdemo.bean.SingleEntity;
import com.example.f8399.testdemo.databinding.ActivityRecyclerViewBinding;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewActivity extends BaseActivity implements View.OnClickListener {

    ActivityRecyclerViewBinding mBinding;
    private RcAdapter mAdapter;
    private List<SingleEntity> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_recycler_view);
        mBinding.setAc(this);

        initView();
    }

    private void initView() {
        mBinding.tvAll.setOnClickListener(this);
        mBinding.tvDel.setOnClickListener(this);
        initData();
    }

    private void initData() {
        mList = new ArrayList<>();

        for (int i = 0; i < 100; i++) {
            SingleEntity entity = new SingleEntity("hello-->" + i, false);
            mList.add(entity);
        }
        mAdapter = new RcAdapter(this, mList);
        mBinding.rv.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rv.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new RcAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(SingleEntity item) {
                ToastUtils.showShort(item.getName());
            }
        });

        mAdapter.setOnSetAllListener(new RcAdapter.OnSetAllListener() {
            @Override
            public void setAll(boolean status) {
                isAll = status;
                if (status) {
                    mBinding.tvAll.setText("取消全选");
                } else {
                    mBinding.tvAll.setText("全选");
                }
            }
        });
    }


    private boolean isAll = false;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_all:
                if (!isAll) {
                    isAll = true;
                    mBinding.tvAll.setText("全选");

                }else {
                    isAll = false;
                    mBinding.tvAll.setText("取消全选");

                }

                for (int i = 0; i < mList.size(); i++) {
                    SingleEntity singleEntity = mList.get(i);
                    singleEntity.setCheck(isAll);
                }
                mAdapter.setList(mList);

                break;

            case R.id.tv_del:
                //list 执行删除，不能直接在原list上进行操作remove，否则会出现只删除一半数据的情况，解决办法
                // 需要借助临时变量或者java 8 的lambda语法才可以，不知道是忘了还是压根就不知道，惭愧
                List<SingleEntity> list = mAdapter.getList();
                List<SingleEntity> temp = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    SingleEntity entity = list.get(i);
                    if (entity.isCheck()) {
                        temp.add(entity);
                    }
                }
                list.removeAll(temp);
                mAdapter.setList(list);
                break;
        }
    }
}
